package com.chebawang.controller;

import com.chebawang.entity.Machine;
import com.chebawang.repository.MachineRepository;
import com.chebawang.entity.Result;
import com.chebawang.util.CommonUtil;
import com.chebawang.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(description = "设备管理")
@RestController
@RequestMapping("machine")
public class MachineController {


    @Autowired
    private MachineRepository machineRepository;


    @ApiOperation(value = "设备管理列表", notes = "设备管理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"page\": \"页码\",\"size\": \"每页数量\"}", required = true, dataType = "String"),
    })
    @PostMapping("/list")
    public Result list(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        int page = tempData.getInt("page");
        int size = tempData.getInt("size");

        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        Pageable pageable = new PageRequest(page, size, sort);
//        String name = tempData.optString("name");

        Page<Machine> atPage = machineRepository.findAll(pageable);
        return ResultUtil.success(atPage);
    }

    @ApiOperation(value = "设备管理列表无分页", notes = "设备管理列表")
    @GetMapping("/list")
    public Result list() {
        List<Machine> blist = machineRepository.findAllByIsdel(false);
        return ResultUtil.success(blist);
    }

    @ApiOperation(value = "设备管理列表", notes = "设备管理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"id\":\"设备id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/findById")
    public Result findById(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);

        String id = tempData.optString("id");

        Machine b = machineRepository.findOne(id);
        return ResultUtil.success(b);


    }


    @ApiOperation(value = "添加设备管理", notes = "添加设备管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"quipmentCode\":\"具体信息\",\"oldman_id\":\"老人id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/save")
    public Result save(@RequestBody String data) throws ParseException {
        JSONObject tempData = new JSONObject(data);
        String quipmentCode = tempData.optString("quipmentCode");
        String equipmentModel = tempData.optString("equipmentModel");
        String equipmentName = tempData.optString("equipmentName");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date checkDate=null;
        Date shipmentsDate=null;
        if(CommonUtil.isNOT_Null(tempData.optString("checkDate"))){
            checkDate=sdf.parse(tempData.getString("checkDate"));
        }
        if(CommonUtil.isNOT_Null(tempData.optString("shipmentsDate"))){
            shipmentsDate=sdf.parse(tempData.getString("shipmentsDate"));
        }
        Machine dc = new Machine();
        dc.setCreateDate(new Date());
        dc.setIsdel(false);
        dc.setCheckDate(checkDate);
        dc.setEquipmentCode(quipmentCode);
        dc.setEquipmentModel(equipmentModel);
        dc.setEquipmentName(equipmentName);
        dc.setShipmentsDate(shipmentsDate);
        return ResultUtil.success(machineRepository.save(dc));
    }

    @ApiOperation(value = "修改设备管理", notes = "修改设备管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"id\":\"设备管理id\",\"memo\":\"具体信息\"}", required = true, dataType = "String"),
    })
    @PostMapping("/edit")
    public Result edit(@RequestBody String data) throws ParseException {
        JSONObject tempData = new JSONObject(data);
        String id = tempData.optString("id");
        String quipmentCode = tempData.optString("quipmentCode");
        String equipmentModel = tempData.optString("equipmentModel");
        String equipmentName = tempData.optString("equipmentName");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date checkDate=null;
        Date shipmentsDate=null;
        if(CommonUtil.isNOT_Null(tempData.optString("checkDate"))){
            checkDate=sdf.parse(tempData.getString("checkDate"));
        }
        if(CommonUtil.isNOT_Null(tempData.optString("shipmentsDate"))){
            shipmentsDate=sdf.parse(tempData.getString("shipmentsDate"));
        }
        Machine dc = machineRepository.findOne(id);
        if (dc == null) {
            return ResultUtil.error(-1, "获取设备管理失败！");
        }
        dc.setCheckDate(checkDate);
        dc.setEquipmentCode(quipmentCode);
        dc.setEquipmentModel(equipmentModel);
        dc.setEquipmentName(equipmentName);
        dc.setShipmentsDate(shipmentsDate);
        return ResultUtil.success(machineRepository.save(dc));
    }

    @ApiOperation(value = "删除设备管理", notes = "删除设备管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"id\": \"设备管理id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/del")
    public Result del(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String id = tempData.getString("id");
        Machine dc = machineRepository.findOne(id);
        if (dc == null) {
            return ResultUtil.error(-1, "获取设备管理失败！");
        }
        dc.setIsdel(true);
        return ResultUtil.success(machineRepository.save(dc));
    }

}

